package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.bean.SessionUser;
import com.maixy.studyonlineapi.entity.Team;
import com.maixy.studyonlineapi.entity.User;
import com.maixy.studyonlineapi.exceptions.MissParamException;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.service.TeamService;
import com.maixy.studyonlineapi.service.UserService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2020/11/1 16:42
 * @Author by maixy
 */
@RestController
@RequestMapping("/v1/user")
public class UserController extends ApiController {
    @Resource
    private UserService userService;
    @Resource
    private TeamService teamService;

    /**
     * 用户注册
     * @param request -- userName：用户名
     *                -- pwd：密码
     *                -- nickName：昵称
     *                -- avatarUrl：头像地址
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    public ApiResponse registered(ApiRequest request) {
        String userName = request.getDataParamAsString("userName");
        String pwd = request.getDataParamAsString("pwd");
        String nickName = request.getDataParamAsString("nickName");
        String avatarUrl = request.getDataParamAsString("avatarUrl");

        if (StringUtils.isBlank(userName) || StringUtils.isBlank(pwd) || StringUtils.isBlank(nickName) || StringUtils.isBlank(avatarUrl)) {
            throw new MissParamException();
        }

        User user = userService.create(userName, pwd, nickName, avatarUrl);
        SessionUser sessionUser = userService.buildUserInfo(user);
        Long teamId = teamService.add(user.getId());
        return ApiResponse.ok()
                .addValueToData("user", sessionUser)
                .addValueToData("teamId",String.valueOf(teamId));
    }

    /**
     * 用户登录
     * @param request -- userName：用户名
     *                -- pwd密码
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    public ApiResponse login(ApiRequest request) {
        String userName = request.getDataParamAsString("userName");
        String pwd = request.getDataParamAsString("pwd");

        if (StringUtils.isBlank(userName) || StringUtils.isBlank(pwd)) {
            throw new MissParamException();
        }

        User user = userService.findUserByUserNameAndPwd(userName, pwd);
        if (null == user || user.getHasDelete()) {
            throw new TipException("用户名或密码不正确，请检查一下");
        }
        SessionUser sessionUser = userService.buildUserInfo(user);
        Team team = teamService.findTeamByUserId(user.getId());
        Long teamId;
        if (null == team || team.getHasDelete()) {
            teamId = teamService.add(user.getId());
        }else {
            teamId = team.getId();
        }
        return ApiResponse.ok()
                .addValueToData("user", sessionUser)
                .addValueToData("teamId",String.valueOf(teamId));
    }

    /**
     * 获得个人的基本信息
     * @param request -- userName：用户名
     *                -- pwd密码
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse getUserBasisInfo(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());


        return ApiResponse.ok()
                .addValueToData("userBasisInfoView", userService.getUserBasisInfo(teamId, userId));
    }

    @Privilege
    public ApiResponse updateNickName(ApiRequest request) {
        String nickName = request.getDataParamAsString("nickName");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (StringUtils.isBlank(nickName)) {
            throw new MissParamException();
        }
        return ApiResponse.ok()
                .addValueToData("user", userService.updateNickName(userId, nickName));
    }

    @Privilege
    public ApiResponse updateAvatarUrl(ApiRequest request) {
        String avatarUrl = request.getDataParamAsString("avatarUrl");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (StringUtils.isBlank(avatarUrl)) {
            throw new MissParamException();
        }
        return ApiResponse.ok()
                .addValueToData("user", userService.updateAvatarUrl(userId, avatarUrl));
    }

    @Privilege
    public ApiResponse updatePwd(ApiRequest request) {
        String oldPwd = request.getDataParamAsString("oldPwd");
        String pwd = request.getDataParamAsString("pwd");
        String confirmPwd = request.getDataParamAsString("confirmPwd");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (StringUtils.isBlank(oldPwd) || StringUtils.isBlank(pwd) || StringUtils.isBlank(confirmPwd)) {
            throw new MissParamException();
        }
        userService.updatePwd(oldPwd, pwd, confirmPwd, userId);
        return ApiResponse.ok();
    }
}
